<?php
//Required for connectDB() and getResourceIDsInContainer
require("dbFunctions.php");

/**
 * Creates a new resource package.
 * A package may only be a container, or be contained. If its a container, it may
 * contain many resources, but a resource may only be contained by one resource.
 * This file should be hidden from users using the web interface and is used to populate the database
 * from the mobile device specifically.
 *
 * Author: Steve Gennaoui
 * Date: 11/06/2012
 */

$container_id = $_REQUEST['container_id'];
$contained_id = $_REQUEST['contained_id'];

if( !idIsValid($container_id,"QR_Code") )
	die("Specified container ID is invalid.");
if( !idIsValid($contained_id,"QR_Code") )
	die("The ID of the resource to be added to the package is invalid.");
if( isPartOfPackage($contained_id) )
	die("The resource to be added to the package is already part of a Package.");
if( !isAbleToBeTopLevel($container_id) )
	die("The specified top level container is not able to the top level.");
if($container_id == $contained_id)
	die("A top level resource cannot add a resource to itself.");

//Get Database handle
$dbh = connectDB();
$query = "INSERT INTO Resource_Packages(container_qr_code,contained_qr_code) ".
		 "VALUES(:container_id,:contained_id)";
$stmt = $dbh->prepare($query);
$stmt->bindParam(':container_id',$container_id);
$stmt->bindParam(':contained_id',$contained_id);
$stmt->execute();

//Release statement and db handle
$stmt=null;
$dbh=null;
?>
